package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.uwlaser.domain.*;
import com.uwlaser.service.AntonService;
import com.uwlaser.service.dto.AntonConfigBantch;
import com.uwlaser.service.dto.AntonConfigResult;
import com.uwlaser.service.dto.AntonRecordProductParam;
import com.uwlaser.service.mapper.AntonConfigDeviceMapper;
import com.uwlaser.service.mapper.AntonConfigMapper;
import com.uwlaser.service.mapper.AntonRecordMapper;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yangwei
 * @create 2022/12/27
 */
@Transactional(propagation = Propagation.SUPPORTS, rollbackFor = Exception.class)
@Service
public class AntonServiceImpl implements AntonService {

    @Autowired
    private  AntonConfigMapper antonConfigMapper;

    @Autowired
    private AntonConfigDeviceMapper antonConfigDeviceMapper;

    @Autowired
    private AntonRecordMapper antonRecordMapper;

    /**
     * 根据设备编号获取安东配置信息
     * @param param
     * @return
     */
    @Override
    public List<AntonConfigResult> getAntonConfigByDeviceCode(BaseParam param) {
        List<AntonConfig> antonConfigList = new ArrayList<>();
        try {
            //查询设备关联安东配置信息
            List<AntonConfigDevice> antonDeviceByDeviceCode = antonConfigDeviceMapper.getAntonDeviceByDeviceCode(param.getDeviceCode());
            //收集安东配置信息
            List<String> antonCode= antonDeviceByDeviceCode.stream().map(AntonConfigDevice::getAntonCode).collect(Collectors.toList());

            QueryWrapper<AntonConfig> queryWrapper = new QueryWrapper<>();
            queryWrapper.in("anton_code",antonCode);
            antonConfigList = antonConfigMapper.selectList(queryWrapper);
        } catch (Exception e) {
            e.printStackTrace();
        }
        List<AntonConfigResult> antonConfigResults = new ArrayList<>();
        if(!CollectionUtils.isEmpty(antonConfigList)){
            antonConfigList.forEach(item->{
                AntonConfigResult antonConfigResult = ConvertUtil.convert(item,AntonConfigResult.class);
                antonConfigResults.add(antonConfigResult);
            });
        }
        return antonConfigResults;
    }

    /**
     * 获取所有设备对应的安东配置信息
     * @return
     */
    @Override
    public List<AntonConfigBantch> getAllAntonConfig() {
        List<AntonConfigBantch> antonConfigBantches = new ArrayList<>();
        try {
            //查询所有设备编号
            List<String> deviceCodes = antonConfigDeviceMapper.getAllDeviceCode();
            for (String deviceCode : deviceCodes) {
                AntonConfigBantch antonConfigBantch =new AntonConfigBantch();
                //根据设备编号查询安东配置编号
                List<String> antonCodes = antonConfigDeviceMapper.getAntonCodeByDevice(deviceCode);
                QueryWrapper<AntonConfig> queryWrapper = new QueryWrapper<>();
                queryWrapper.in("anton_code",antonCodes);
                List<AntonConfig> antonConfigList = antonConfigMapper.selectList(queryWrapper);

                List<AntonConfigResult> antonConfigResults = new ArrayList<>();
                if(!CollectionUtils.isEmpty(antonConfigList)){
                    antonConfigList.forEach(item->{
                        AntonConfigResult antonConfigResult = ConvertUtil.convert(item,AntonConfigResult.class);
                        antonConfigResults.add(antonConfigResult);
                    });
                }
                antonConfigBantch.setAntonConfigResultList(antonConfigResults);
                antonConfigBantch.setDeviceCode(deviceCode);
                antonConfigBantches.add(antonConfigBantch);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return antonConfigBantches;
    }

    /**
     * 上传安东记录信息
     * @param param
     * @return
     */
    @Override
    public ResultEntity uploadAntonInfo(AntonRecordProductParam param) {
        try {
            //校验设备对应的安东配置信息是否正确
            QueryWrapper<AntonConfigDevice> queryWrapper = new QueryWrapper();
            queryWrapper.eq("anton_code",param.getAntonCode());
            queryWrapper.eq("device_code",param.getDeviceCode());
            List<AntonConfigDevice> antonConfigDevices = antonConfigDeviceMapper.selectList(queryWrapper);
            if(CollectionUtils.isEmpty(antonConfigDevices)){
                //集合为空 该设备与安东配置信息不符
                throw new RuntimeException("当前设备："+param.getDeviceCode()+"不包含编码为:"+param.getAntonCode()+"的安东配置信息!");
            }
            //校验安东配置是否被禁用
            QueryWrapper<AntonConfig> antonConfigQueryWrapper = new QueryWrapper<>();
            antonConfigQueryWrapper.eq("anton_code",param.getAntonCode());
            AntonConfig antonConfig = antonConfigMapper.selectOne(antonConfigQueryWrapper);
            if("0".equals(antonConfig.getIsDisable())){
                throw new RuntimeException("当前设备："+param.getDeviceCode()+"编码为:"+param.getAntonCode()+"的安东配置信息已经被禁用");
            }
            //转换成安东记录表实体
            AntonRecord antonRecord = ConvertUtil.convert(param, AntonRecord.class);
            //添加安东纪录
            int resultCount = antonRecordMapper.insert(antonRecord);
            if(resultCount>0){
                return new ResultEntity(new Object(),"上传安东记录成功");
            }else{
                return new ResultEntity(new Object(),"添加失败",HttpStatus.INTERNAL_SERVER_ERROR);
            }
        } catch (RuntimeException e) {
            ResultEntity resultEntity = new ResultEntity();
            resultEntity.setData("");
            resultEntity.setStatus(HttpStatus.INTERNAL_SERVER_ERROR.value());
            resultEntity.setMessage("当前操作发生异常，设备编号："+param.getDeviceCode()+",异常信息："+e.getMessage());
            resultEntity.setTimestamp(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            return resultEntity;
        }
    }
}
